package com.znsd.zl.house.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.znsd.zl.house.entity.HouseImageEntity;
import com.znsd.zl.house.service.HouseImageService;
import com.znsd.zl.utils.BaseController;
import com.znsd.zl.utils.PageUtils;
import com.znsd.zl.utils.R;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @ClassName THouseImage
 * @Description 描述
 * @author jaypdd
 * @email jaypdd@163.com
 * @date 2020-08-26 23:20:43
 * @Version v1.0
 */
@RestController
@RequestMapping("/zl/houseImage")
public class HouseImageController extends BaseController<HouseImageService, HouseImageEntity,Integer> {
	/**
	 * 查询轮播图
	 * @return
	 */
	@RequestMapping("firstFive")
	public R firstFive(){
		List<HouseImageEntity> houseImageEntities = service.firstFive();
		return R.ok().put("list",houseImageEntities);

	}

	/**
	 * 设置轮播图
	 * @param id
	 * @return
	 */
	@RequestMapping("slideshow/{id}")
	public R slideshow(@PathVariable("id") Integer id){
		String s = service.setSlideshow(id);
		return R.ok(s);
	}


	/**
	 * 分页查询
	 * @param pageNum  当前请求页码
	 * @param pageSize 请求的记录数
	 */
	@Override
	@GetMapping("/{pageNum}/{pageSize}")
	public R pageFind(@RequestParam Map<String, Object> params,
	                  @PathVariable(name = "pageNum") Integer pageNum,
	                  @PathVariable(name = "pageSize") Integer pageSize) {
		PageUtils pageUtils = null;
		try{
			Object houseId=params.get("houseId");
			QueryWrapper<HouseImageEntity> queryWrapper = new QueryWrapper<>();
			if(houseId!=null){
				if (houseId.toString().length()>0){
					queryWrapper.eq("house_id",houseId);
				}
			}

			pageUtils = new PageUtils(service.page(new Page<>(pageNum, pageSize), queryWrapper));
		}catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器异常，请稍后重试");
		}
		return R.ok().put("page", pageUtils);
	}


}
